package com.ssy.lingxi.purchase.controller.bidding;

import com.ssy.lingxi.common.constant.manage.ShopTypeEnum;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.response.AttributeResponse;
import com.ssy.lingxi.purchase.api.dto.request.*;
import com.ssy.lingxi.purchase.api.dto.request.bidding.*;
import com.ssy.lingxi.purchase.api.dto.request.online.DiscardRequest;
import com.ssy.lingxi.purchase.api.dto.request.purchase.PurchaseListRequest;
import com.ssy.lingxi.purchase.api.dto.response.BiddingExternalWorkStateEnumResponse;
import com.ssy.lingxi.purchase.api.dto.response.bidding.*;
import com.ssy.lingxi.purchase.api.dto.response.online.OnlineBiddingListResponse;
import com.ssy.lingxi.purchase.api.dto.response.online.OnlineMaterielResponse;
import com.ssy.lingxi.purchase.api.dto.response.online.OnlineProductResponse;
import com.ssy.lingxi.purchase.api.dto.response.purchase.PurchaseListResponse;
import com.ssy.lingxi.purchase.api.dto.response.purchase.SearchReSourceListResponse;
import com.ssy.lingxi.purchase.entity.onlinebidding.OnLineBiddingProduct;
import com.ssy.lingxi.purchase.entity.onlinebidding.OnlineMateriel;
import com.ssy.lingxi.purchase.service.bidding.BiddingService;
import org.apache.commons.lang.StringUtils;
import org.modelmapper.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @menu 采购能力 - 采购竞价
 * @author wrc
 * @version 2.0.0
 * @date 2021/03/23
 */
@RestController
@RequestMapping("/purchase/bidding")
public class PurchaseBiddingController extends BaseController {

    @Resource
    BiddingService biddingService;

    @Autowired
    private HttpServletRequest request;

    /**
     *采购竞价单查询
     */
    @GetMapping("/list")
    public Wrapper<PageData<BiddingListResponse>> biddingList(BiddingListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingList(request,sysUser);
    }

    /**
     *采购竞价单详情
     */
    @GetMapping("/details")
    public Wrapper<BiddingDetailsResponse> biddingDetails(BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingDetails(request,sysUser);
    }

    /**
     *采购物料分页查询
     */
    @GetMapping("/materiel/page")
    public  Wrapper<PageData<BiddingProductRequest>>  materielPage( BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.materielPage(request,sysUser);
    }

    /**
     *需求对接会员列表分页查询
     */
    @GetMapping("/member/page")
    public  Wrapper<PageData<BiddingMemberResponse>>  memberPage(BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.memberPage(request,sysUser);
    }

    /**
     *报价明细-分页
     */
    @GetMapping("/quoted/price/detaild")
    public  Wrapper<PageData<OnlineProductResponse>>  quotedPriceDetaild(OnlineDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.quotedPriceDetaild(request,sysUser);
    }

    /**
     *作废竞价单
     */
    @PostMapping("/discard")
    public  Wrapper<Void>  discard(@Validated @RequestBody DiscardRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.discard(request,sysUser);
    }

    /**
     *待新增采购竞价单列表
     * 待新增采购竞价单
     * @param request 分页列表查询请求参数
     * @param httpServletRequest 请求
     * @return 待新增 采购竞价单分页列表
     */
    @GetMapping("/await/new/list")
    public Wrapper<PageData<BiddingListResponse>> awaitNewList(BiddingListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.awaitNewList(request,sysUser);
    }

    /**
     * 待新增商城采购竞价单
     * @param request 分页列表查询请求参数
     * @param httpServletRequest
     * @return 待新增商城采购竞价单分页列表
     */
    @GetMapping("/await/shop/new/list")
    public Wrapper<PageData<BiddingListResponse>> awaitShopNewList(BiddingListRequest request,HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.awaitShopNewList(request,sysUser);
    }

    /**
     *添加采购竞价单-待新增采购竞价单
     */
    @PostMapping("/add")
    public  Wrapper<Void> add(@Validated @RequestBody BiddingAddRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.add(request,sysUser, modelMapper);
    }

    /**
     *修改采购竞价单-待新增采购竞价单
     */
    @PostMapping("/update")
    public  Wrapper<Void> update(@Validated @RequestBody BiddingUpdateRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.update(request,sysUser,modelMapper);
    }

    /**
     *删除采购竞价单-待新增采购竞价单
     */
    @PostMapping("/delete")
    public  Wrapper<Void>  delete(@Validated @RequestBody BaseRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.delete(request,sysUser);
    }

    /**
     *批量删除采购竞价单-待新增采购竞价单
     */
    @PostMapping("/delete/batch")
    public  Wrapper<Void>  deleteBatch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.deleteBatch(request,sysUser);
    }

    /**
     *提交待审核采购竞价单-待新增采购竞价单
     */
    @PostMapping("/examine")
    public Wrapper<Void> biddingExamine(@Validated @RequestBody BaseRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingExamine(request,sysUser);
    }

    /**
     *批量提交待审核采购竞价单-待新增采购竞价单
     */
    @PostMapping("/examine/batch")
    public Wrapper<Void> biddingExamineBatch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingExamineBatch(request,sysUser);
    }

    /**
     *采购竞价-待审核采购竞价单(一级)
     */
    @GetMapping("/stay/examine/list1")
    public Wrapper<PageData<BiddingListResponse>> biddingStayExamineList1(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingStayExamineList1(request,sysUser);
    }

    /**
     *采购竞价-审核采购竞价单(一级)
     */
    @PostMapping("/examine1")
    public Wrapper<Void> biddingExamine1(@Validated @RequestBody BaseExamineRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingExamine1(request,sysUser);
    }

    /**
     *采购竞价-审核采购竞价单(一级)批量
     */
    @PostMapping("/examine1/batch")
    public Wrapper<Void> biddingExamine1Batch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingExamine1Batch(request,sysUser);
    }

    /**
     *采购竞价-待审核采购竞价单(二级)
     */
    @GetMapping("/stay/examine/list2")
    public Wrapper<PageData<BiddingListResponse>> biddingStayExamineList2(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingStayExamineList2(request,sysUser);
    }

    /**
     *采购竞价-审核采购竞价单(二级)
     */
    @PostMapping("/examine2")
    public Wrapper<Void> biddingExamine2(@Validated @RequestBody BaseExamineRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingExamine2(request,sysUser);
    }

    /**
     *采购竞价-审核采购竞价单(二级)批量
     */
    @PostMapping("/examine2/batch")
    public Wrapper<Void> biddingExamine2Batch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingExamine2Batch(request,sysUser);
    }

    /**
     *采购竞价-待提交采购竞价单
     */
    @GetMapping("/stay/submit/list")
    public Wrapper<PageData<BiddingListResponse>> biddingStaySubmitList(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingStaySubmitList(request,sysUser);
    }

    /**
     *采购竞价-提交采购竞价单
     */
    @PostMapping("/submit")
    public Wrapper<Void> biddingSubmit(@Validated @RequestBody BaseExamineRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingSubmit(request,sysUser);
    }

    /**
     *采购竞价-批量提交采购竞价单
     */
    @PostMapping("/submit/batch")
    public Wrapper<Void> biddingSubmitBatch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingSubmitBatch(request,sysUser);
    }

    /**
     * 待审核竞价报名
     */
    @GetMapping("/stay/examin/bidding/list")
    public Wrapper<PageData<OnlineBiddingListResponse>> stayExaminBiddingList(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.stayExaminBiddingList(request,sysUser);
    }

    /**
     * 审核竞价报名
     */
    @PostMapping("/examin/bidding/signup")
    public Wrapper<Void> stayExaminBiddingSignup(@Validated @RequestBody  BiddingAudteRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.stayExaminBiddingSignup(request,sysUser);
    }

    /**
     * 待竞价
     */
    @GetMapping("/stay/bidding/list")
    public Wrapper<PageData<BiddingListResponse>> stayBiddingList(@Validated  BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.stayBiddingList(request,sysUser);
    }

    /**
     * 竞价管理
     */
    @PostMapping("/manage/bidding")
    public Wrapper<ManageBiddingResponse> manageBidding(@Validated  @RequestBody BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.manageBidding(request,sysUser);
    }

    /**
     * 竞价动态-竞价管理
     */
    @GetMapping("/dynamic/bidding")
    public Wrapper<BiddingDynamicResponse> dynamicBidding(@Validated  BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.dynamicBidding(request,sysUser);
    }

    /**
     * 竞价排名-竞价管理
     */
    @GetMapping("/ranking/bidding")
    public Wrapper<List<QuotationRankingResponse>> rankingBidding(@Validated  BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.rankingBidding(request,sysUser);
    }

    /**
     * 报名会员-竞价管理
     */
    @GetMapping("/signup/member")
    public Wrapper<List<SginUpInfoResponse>> signupMember(@Validated  BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.signupMember(request,sysUser);
    }

    /**
     * 报价台-竞价物料&-竞价管理
     */
    @GetMapping("/bidding/materiel")
    public Wrapper<List<AwardProcessResponse>> biddingMateriel(@Validated  BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.biddingMateriel(request,sysUser);
    }

    /**
     * 获取采购竞价单内部流程状态
     */
    @GetMapping("/interior/status")
    public Wrapper<List<BiddingExternalWorkStateEnumResponse>> interiorStatus() {
        return biddingService.interiorStatus();
    }

    /**
     * 获取采购竞价单外部流程状态
     */
    @GetMapping("/external/status")
    public Wrapper<List<BiddingExternalWorkStateEnumResponse>> externalStatus() {
        return biddingService.externalStatus();
    }


/********************************************* 审核竞价结果 *************************************************************************/
    /**
     *待提交审核竞价结果
     */
    @GetMapping("/stay/submit/bidding/list")
    public Wrapper<PageData<StaySubmitBiddingListResponse>> staySubmitBiddingList(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBiddingList(request,sysUser);
    }

    /**
     *提交审核竞价结果详情
     */
    @GetMapping("/stay/submit/details")
    public Wrapper<StaySubmitDetailsResponse> staySubmitDetails(BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitDetails(request,sysUser);
    }

    /**
     *提交审核竞价结果
     */
    @PostMapping("/submit/examine/bidding/return")
    public Wrapper<Void> submitExamineBiddingReturn(@Validated @RequestBody SubmitExamineBiddingReturnRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.submitExamineBiddingReturn(request,sysUser);
    }

    /**
     *修改审核竞价结果
     */
    @PostMapping("/update/bidding/return")
    public Wrapper<Void> updateBiddingReturn(@Validated @RequestBody SubmitExamineBiddingReturnRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.updateBiddingReturn(request,sysUser);
    }

    /**
     *待审核竞价结果（一级）
     */
    @GetMapping("/stay/submit/bidding/list1")
    public Wrapper<PageData<StaySubmitBiddingListResponse>> staySubmitBiddingList1(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBiddingList1(request,sysUser);
    }

    /**
     *审核竞价结果（一级）
     */
    @PostMapping("/submit/bidding1")
    public Wrapper<Void> staySubmitBidding1(@Validated @RequestBody BaseExamineRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBidding(request,sysUser);
    }

    /**
     *审核竞价结果（一级）批量
     */
    @PostMapping("/submit/bidding1/batch")
    public Wrapper<Void> staySubmitBidding1Batch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBidding1Batch(request,sysUser);
    }

    /**
     *待审核竞价结果（二级）
     */
    @GetMapping("/stay/submit/bidding/list2")
    public Wrapper<PageData<StaySubmitBiddingListResponse>> staySubmitBiddingList2(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBiddingList2(request,sysUser);
    }

    /**
     *审核竞价结果（二级）
     */
    @PostMapping("/submit/bidding2")
    public Wrapper<Void> staySubmitBidding2(@Validated @RequestBody BaseExamineRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBidding2(request,sysUser);
    }

    /**
     *审核竞价结果（二级）批量
     */
    @PostMapping("/submit/bidding2/batch")
    public Wrapper<Void> staySubmitBidding2Batch(@Validated @RequestBody BaseAllRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.staySubmitBidding2Batch(request,sysUser);
    }

    /**
     * 待确认竞价结果
     */
    @GetMapping("/stay/confirm/bidding/list")
    public Wrapper<PageData<StaySubmitBiddingListResponse>> stayConfirmBiddingList(BiddingListRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.stayConfirmBiddingList(request,sysUser);
    }

    /**
     * 确认竞价结果
     */
    @PostMapping("/stay/confirm/bidding")
    public Wrapper<Void> stayConfirmBidding(@Validated @RequestBody BaseExamineRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.stayConfirmBidding(request,sysUser);
    }

    /**
     * 采购商机筛选-竞价单查询
     */
    @GetMapping("/business/opportunity/bidding/list")
    public Wrapper<PageData<PurchaseListResponse>> businessPpportunityBiddingList(@Validated PurchaseListRequest request) {
        return biddingService.businessPpportunityBiddingList(request);
    }

    /**
     * 采购公示
     */
    @GetMapping("/purchase/make/list")
    public Wrapper<PageData<PurchaseMakeList>> purchaseMakeList(PurchaseMakeListRequest request){
        return biddingService.purchaseMakeList(request);
    }

    /**
     *采购寻源--采购竞价--采购竞价详情
     */
    @GetMapping("/search/source/details")
    public Wrapper<BiddingDetailsResponse> searchReSourceDetails(BaseDetailsRequest request, HttpServletRequest httpServletRequest) {
        UserLoginCacheDTO sysUser = this.getSysUser(httpServletRequest);
        return biddingService.searchReSourceDetails(request, sysUser);
    }

    /**
     *采购物料分页查询
     */
    @GetMapping("/search/source/materiel/page")
    public  Wrapper<PageData<BiddingProductRequest>>  searchSourceMaterielPage( BaseDetailsRequest request) {
        return biddingService.searchSourceMaterielPage(request);
    }

    /**
     * 分页查询在线竞标-中标的采购物料
     * @param pageVO 分页实体
     * @param id 竞标id
     */
    @GetMapping("/prize/materiel/page")
    public Wrapper<PageData<OnlineMaterielResponse>> prizeMaterielPage(PageVO pageVO, @RequestParam("id") Long id) {
        PageData<OnLineBiddingProduct> pageData = biddingService.prizeMaterielPage(pageVO, id);
        Long totalCount = pageData.getTotalCount();
        if(totalCount == null || totalCount <= 0){
            return Wrapper.success(new PageData<>(totalCount, new ArrayList<>()));
        }
        List<OnlineMaterielResponse> responses = modelMapper.map(pageData.getData(), new TypeToken<List<OnlineMaterielResponse>>() {}.getType());
        return Wrapper.success(new PageData<>(totalCount, responses));
    }

    /*****************************************************  采购门户  ********************************************************************/
    /**
     *商城能力--企业直采--采购门户--采购寻源筛选-接口
     */
    @GetMapping("/search/source/list")
    public Wrapper<PageData<SearchReSourceListResponse>> searchReSourceList(PurchaseListRequest purchaseListRequest ) {
        Long shopId = StringUtils.isEmpty(request.getHeader("shopId")) ? null : Long.parseLong(request.getHeader("shopId"));
        return biddingService.searchReSourceList(purchaseListRequest, shopId, this.getSysUser());
    }
}
